package org.kjtc.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kjtc.entity.Order;
import org.kjtc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;



    @Autowired
    OrderService orderService;

    @RequestMapping("/qrcode")
    public String qrlist() {
        return "qrcode";
    }
    @RequestMapping("/chargepilestatus")
    public String chargestatus() {
        return "chargepilestatus";
    }

    @RequestMapping("/orderStatus")
    public String orderStatus() {
        return "orderStatus";
    }

    @RequestMapping("/orderLog")
    public String orderLogPage(){
        return "orderLog";
    }
    @RequestMapping("/orderHistory")
    public String orderHistoryPage(){
        return "orderHistory";
    }

    @RequestMapping("/service/getOrderStatus")
    @ResponseBody
    public Object loginCheck(HttpServletRequest request, HttpServletResponse response){

        Map<String, Object> map = new HashMap<String, Object>();

        List<Order> order = orderService.getUserList();
        map.put("list",order);
        return map;
    }

    @RequestMapping("/createOrder")
    public Object createOrder(HttpServletRequest request, HttpServletResponse response){

        List<Order> list = new ArrayList<Order>();

        for(int i=0;i<5;i++){
            Order o =new Order();
            o.setOrderNumber("123456");
            o.setUserId("1");
            o.setChargingStationID("412312313");
            o.setEquipmentID("sdflsd");
            o.setCreateDTTM(new Timestamp(System.currentTimeMillis()));
            list.add(o);
        }

        for(int i=0;i<5;i++){
           Order o = list.get(i);
           o.setOrderNumber(i+"#");
            redisTemplate.opsForValue().append(o.getOrderNumber()+"orderkey",(new JSONObject().fromObject(o)).toString());
        }


        JSONArray js = new JSONArray();
        for(int i=0;i<5;i++){
            js.add(new JSONObject().fromObject(redisTemplate.opsForValue().get(i+"#orderkey")));
        }
        return js;
    }

}
